<?php
// 产生微信APP支付数据
// 订单数据
require_once $this->website['path']['action'] . '/app/pay/data.php';
// 以下是 data.php 中的变量
// $json
// $title       ---商品名称
// $totals      ---支付总价
// $orderCode   ---订单号
// $orderid     ---所有订单号
// $num         ---订单个数---判断是否是多个订单一起支付

require_once $this->website['path']['lib'] . '/wx/pay3/lib/WxPay.Api.php';
// require_once $this->website['path']['lib'] . '/wx/pay/lib/WxPay.Api.php';

$configura = fn_configura($this->website, 'sys');

$Config = new WxPayConfig(
	array(
		'APPID' => $configura['payAppidWx'],
		'MCHID' => $configura['payMchidWx'],
		'NotifyUrl' => $configura['payWxNotifyUrl'],
		'APPSECRET' => $configura['payWxAppSecret'],
		'KEY' => $configura['payKeyWx'],
		'Apiclient_cert' => $this->website['path']['conf'] . '/crt/wx/apiclient_cert.pem',
		'Apiclient_key' => $this->website['path']['conf'] . '/crt/wx/apiclient_key.pem',
	)
);

$tm = $_SERVER['REQUEST_TIME'];

$input = new WxPayUnifiedOrder();
$input->SetBody($title); //设置商品或支付单简要描述
$input->SetAttach(urlencode(json_encode($orderid))); //设置附加数据，在查询API和支付通知中原样返回，该字段主要用于商户携带订单的自定义数据
$input->SetOut_trade_no($orderCode); //设置商户系统内部的订单号,32个字符内、可包含字母, 其他说明见商户订单号
$input->SetTotal_fee($totals); //设置订单总金额，只能为整数，详见支付金额
$input->SetTime_start(date("YmdHis", $tm)); //设置订单生成时间，格式为yyyyMMddHHmmss
$input->SetTime_expire(date("YmdHis", $tm + 60 * 10)); //设置订单失效时间，格式为yyyyMMddHHmmss
$input->SetGoods_tag(''); //设置商品标记，代金券或立减优惠功能的参数，说明详见代金券或立减优惠
$input->SetNotify_url('http://app.tuerduo.net/callback/wxpay/'); //设置接收微信支付异步通知回调地址
$input->SetTrade_type('APP'); //设置类型如下：JSAPI，NATIVE，APP
// $order_data = WxPayApi::unifiedOrder($input); //统一下单
$order_data = WxPayApi::unifiedOrder($Config, $input); //统一下单 v3

$json['config'] = $order_data;

$str = 'appid=' . $order_data['appid'] .
	'&nonceStr=' . $order_data['nonce_str'] .
	'&package=' . 'Sign=WXPay' .
	'&partnerId=' . $configura['payMchidWx'] .
	'&prepayid=' . $order_data['prepay_id'] .
	'&timestamp=' . $tm;

$arr = array(
	'appid' => $order_data['appid'],
	'noncestr' => $order_data['nonce_str'],
	'package' => 'Sign=WXPay',
	'partnerid' => $order_data['mch_id'],
	'prepayid' => $order_data['prepay_id'],
	'timestamp' => $tm,
);

//重新生成签名
// $arr['sign'] = strtoupper(md5($str . '&key=' . $configura['payKeyWx']));
// $arr['sign'] = strtoupper(hash_hmac("sha256", $str . '&key=' . $configura['payKeyWx'], $configura['payKeyWx']));
$arr['sign'] = fn_wxGetSign($arr, $configura['payKeyWx']);

$json['data'] = $arr;

$this->json_encode_encrypt($json);